const app = getApp();
const regeneratorRuntime = global.regeneratorRuntime = require("../libs/regenerator");
const co = require('../libs/co');
let sliderWidth = 76;
let pageIndex = 0;
let pageSize = 5;
Page({
  data:{
    category: [
            {name:'全部',id:'all',val:-1},
            {name:'待付款',id:'unpay',val:1},
            {name:'待发货',id:'payed',val:3},
            {name:'待收货',id:'send',val:4},
            {name:'已完成',id:'finish',val:5},
            {name:'退款/售后',id:'service',val:7}
        ],
    detail:[],
    curIndex: 0,
    isScroll: false,
    toView: 'all',
    selectedMenuIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    scrollHeight: 0,
    scrollTop:0,
    hidden: false,
    orders:[],
    hasMore:true,
    isLoad:true
  },
  switchTab:function(e){
        const self = this;
        this.setData({
          isScroll: true
        })
        setTimeout(function(){
          self.setData({
            toView: e.target.dataset.id,
            curIndex: e.target.dataset.index
          })
        },0)
        setTimeout(function () {
          self.setData({
            isScroll: false
          })
        },1)
  },
  onReady() {
     /*let detail = this.data.detail;
     detail = [
       {id:"all",text:"全部"},
       {id:"unpay",text:"待付款"}
     ];
     this.setData({
       detail: detail
     })*/
  },
  onLoad: function(options) {
    app.launchOptions = options;
    let index = options.index;
    this.setData({
      curIndex: index,
    });
    var that = this;
     wx.getSystemInfo({
         success: function(res) {
             that.setData({
                 sliderLeft: (res.windowWidth / that.data.category.length - sliderWidth) / 2,
                 sliderOffset: res.windowWidth / that.data.category.length * that.data.curIndex,
                 scrollHeight: res.windowHeight - 60
             });
         }
     });
     //this.loadProduct();
      /* !Do not Remove the follow line, used for build tool.*/
      //let skuIds = options.skuId;
      //this.loadProduct(skuIds);
  },
  //页面滑动到底部
   bindDownLoad:function(){
     if (this.data.hasMore && this.data.isLoad) {
       this.setData({
         hidden:false,
         isLoad: false
       });
       this.loadProduct();
       console.log("height:" + this.data.scrollHeight);
     }
   },
   scroll:function(event){
     //该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。
      this.setData({
          scrollTop : event.detail.scrollTop,
          hidden: false
      });
   },
   topLoad:function(event){
     //该方法绑定了页面滑动到顶部的事件，然后做上拉刷新
     if (this.data.hasMore) {
       pageIndex = 0;
       this.setData({
           orders:[],
           scrollTop : 0,
           hidden:false
       });
       this.loadProduct();
     }
  },
  loadProduct: function () {
    const self = this;
    co(function *() {
        let type = self.data.category[self.data.curIndex].val;
        let orderService = require("../services/mall/orderService");
        let orders = yield orderService.list(pageIndex,pageSize,type);
        if (orders && orders.length > 0) {
          var list = self.data.orders;
          //list.concat(orders);
          for(var i = 0; i < orders.length; i++){
            list.push(orders[i]);
          }
          pageIndex++;
          self.setData({
            orders: list,
            hidden: true,
            isLoad: true
          });
        } else {
          self.setData({
            hasMore:false,
            isLoad: true
          });
        }
    });
  },
  onShow:function(){
    pageIndex = 0;
    this.setData({
      orders: [],
      hidden: false,
      hasMore: true
    });
    this.loadProduct();
  },
  onMenuTouched: function(e) {
    pageIndex = 0;
    this.setData({
           hidden: false,
           hasMore: true,
           sliderOffset: e.currentTarget.offsetLeft,
           curIndex: e.target.dataset.index,
           orders:[]
    });
    this.loadProduct();
  },
  toPay() {
    wx.showModal({
      title: '提示',
      content: '本系统只做演示，支付系统已屏蔽',
      text:'center',
      complete() {
        wx.switchTab({
          url: '/page/component/user/user'
        })
      }
    })
  },
  clickitem: function (e) {   //带着specId 去详情界面
    var specId = e.currentTarget.dataset.specid;
    wx.navigateTo({
      url: "/pages/page9?id=" + specId,
    })
  },
  bindOrderInfo: function (e) {
    var orderNum = e.currentTarget.dataset.num;
    wx.navigateTo({
      url: "/pages/page12?orderNum=" + orderNum,
    })
  },
  onTouchedOrder:function(e) {
    let orderNum = e.currentTarget.dataset.no;
    let status = e.currentTarget.dataset.t;

    if (status.toUpperCase() == "CANCEL_ORDER" || status.toUpperCase() == "DEL_ORDER" || status.toUpperCase() == "COMFIRM_RECEIVE") {
      this.showModel(orderNum,status);
    } else if(status.toUpperCase() == "APPLY_REFUND") {
      wx.navigateTo({
        url: "/pages/page13?orderNum=" + orderNum,
      })
    } else if(status.toUpperCase() == "CHECK_ORDER") {
      wx.navigateTo({
        url: "/pages/page12?orderNum=" + orderNum,
      })
    }
  },
  showModel: function (orderNum,status){
    let msgText = "是否确认取消此订单?";
    if (status.toUpperCase() == "COMFIRM_RECEIVE") {
       msgText = "是否确认收货?";
    } else if (status.toUpperCase() == "DEL_ORDER") {
       msgText = "是否确认删除此订单?";
    }
    const self = this;

    wx.showModal({
          title: '提示',
          content: msgText,
          confirmText: "确定",
          cancelText: "取消",
          success: function (res) {
              if (res.confirm) {
                  self.doOrder(orderNum,status);
              }else{
              }
          }
      });
  },
  doOrder: function (orderNum,status) {
    this.setData({
      hidden:false
    });
    const self = this;
    co(function *() {
        let orderService = require("../services/mall/orderService");
        let ret = yield orderService.doOrder(orderNum,status);
        console.log(ret);
        if (ret) {
          pageIndex = 0;
          console.log(pageIndex);
          self.setData({
            orders: [],
            hidden: false,
            hasMore: true
          });
          self.loadProduct();
        }
    });
  }
})
